#pragma once
#include"HashTable.h"
using namespace wzy;

namespace wzy3
{
	template<class K, class V, class Set = Setof<K>>
	class MyMap
	{
		typedef HashNode<pair<K, V>> Node;

	public:
		struct getkey
		{
			const K& operator()( const pair<K, V>& data)
			{
				return data.first;
			}
		};

		typedef typename HashTable<K, pair<const K, V>, getkey, Set>::iterator iterator;
		typedef typename HashTable<K, pair<const K, V>, getkey, Set>::const_iterator const_iterator;

		iterator begin() 
		{
			return hash.begin();
		}

		iterator end()
		{
			return hash.end();
		}

		//iterator begin() const
		//{
		//	return hash.begin();
		//}

		//iterator end() const
		//{
		//	return hash.end();
		//}


		bool Insert(const pair<const K, V>& kv)
		{
			return hash.Insert(kv);
		}

		bool Erase(const K& key)
		{
			return hash.Erase(key);
		}

		void Print()
		{
			for (size_t i = 0; i < hash._tables.size(); i++)
			{
				cout << i << "->";
				Node* cur = hash._tables[i];
				while (cur)
				{
					cout << cur->_data.first << ":" << cur->_data.second<<"  ";
					cur = cur->_next;
				}
				cout << endl;
			}
			cout << hash._tables.size() << endl;
		}

		V& operator[](const K& key)
		{
			getkey get;
			Set set;

			size_t hashi = set(get(key)) % hash._tables.size();
			Node* cur = hash._tables[hashi];
			while (cur)
			{
				if (cur->_data.first == key)
				{
					return cur->_data.second;
				}
				cur = cur->_next;
			}
			Insert(make_pair<key, V()>);
			return V();
		}


	private:
		HashTable<K, pair<const K, V>, getkey,Set> hash;

	};
}
